* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"

grstyle set graphsize 3in 3in
* -------------------------------------------------------------------------------------------------
* Prepare data for figures
* -------------------------------------------------------------------------------------------------

* DI entrant outcomes, by x, where x = 
*   i.covstart_year          = calendar year of Medicare entry
*   c.unemprateatapp         = unemployment rate at time of application
*   i.unemp_q20              = ventiles of unemprateatapp
*   i.age_year_covstart_fill = age in years at Medicare entry

* Sample
*   main: individuals who enter Medicare at ages 20-62, and annual Medicare spending after first (usually partial) year of Medicare coverage
*   2262: main, but individuals who enter Medicare at ages 22-62
local sample 2262

* Sample definitions:
* - main (all ages, all years)
local sample_main 1
* - ages 22-62
local sample_2262 inrange(age_year_covstart_fill, 22, 62)
* - ages 51-52
local sample_5152 inrange(age_year_covstart_fill, 51, 52)
* - first full year
local sample_ffy (rfrnc_yr == init_yr + 1)

* Which controls to include in each regression
local controls_01 i.years_since_covstart
local controls_02 i.years_since_covstart##i.fipscounty_firstnm_g

* DI entrant cohort average Medicare spending
local clust county_mofd
foreach y in male_init {
foreach x in covstart_year {
  * local y male_init
  * local x covstart_year
  
  * Control variables
  local ctrl 02
  *assert "`controls_`ctrl''" == "i.years_since_covstart"
  
  * Confirm sample
  assert "`sample'" == "2262"
  
  * Load estimates
  estimates use "$SSDIMed/results/estimates/x-i.`x'/x-i.`x'_y-`y'_controls-`ctrl'_cluster-`clust'_sample-`sample'.ster"
  
  * QC: confirm controls
  * assert "`e(absvars)'" == "i.years_since_covstart"
  
  * Calculate y effect, CI for each level of x variable (_cons plus x-var effect)
  local addvar 0, 0, 0
  local names : colfullnames e(b)
  foreach name of local names {
    if "`name'" == "_cons" continue
    *local val = subinstr("`name'", "b.`=subinstr("`x'", "i.", "", .)'", "", .)
    *local val = subinstr("`val'",  ".`=subinstr("`x'", "i.", "", .)'", "", .)
    lincom _cons + `name'
    local addvar `addvar', `"`name' + _cons"', `r(estimate)', `r(se)'
  }
  regsave, addvar(`addvar') detail(all)
  
  * which variables to keep
  drop if var == "0"
  keep if strpos(var, "+ _cons") > 0
  
  * confidence intervals for added variables
  gen double t_ci = abs(invttail(`e(df_r)', (1 - 0.95) / 2))
  qui gen double ci_lower = coef - t_ci*stderr
  qui gen double ci_upper = coef + t_ci*stderr
  rename (coef ci_lower ci_upper) (`y' `y'_ci_lo `y'_ci_hi)
  keep var `y' `y'_ci_lo `y'_ci_hi clustvar
  
  * numeric level for the x-variable (a factor)
  gen `x' = substr(var, 1, strpos(var, "."))
  replace `x' = regexr(`x', "(b\.)|(\.)", "")
  destring `x', replace
  order `x', after(var)
  
  * Units
  * tot_pmt in $1,000
  * mortality in deaths per 10,000
  foreach var in `y' `y'_ci_lo `y'_ci_hi {
    if "`y'" == "tot_pmt"        replace `var' = `var'/1000
    if strpos("`y'", "died") > 0 replace `var' = `var'*10000
  }
  
  * Save
  tempfile `x'_`y'
  save ``x'_`y'', replace
  list, sep(0)
}
}

* DI incidence
local y incidence
foreach x in covstart_year {  
  * Load data
  use "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", clear
  gisid fipscounty_firstnm covstart_month age_year_covstart_fill, missok
  sum count
  assert r(sum) == 16240776
  qui sum age_year_covstart_fill
  assert r(min)==20 & r(max)==62
  
  * Select sample
  assert `sample_`sample'' == inrange(age_year_covstart_fill, 22, 62)
  keep if `sample_`sample''
  sum count
  assert r(sum) == 15790262

  * Collapse
  gcollapse (mean) nat_unemp_mean=unemp_rate_national_atapp  incidence=incidence_pop_age_atapp [aw = pop_age_atapp], by(`x') fast 
  
  * Save
  tempfile `x'_`y'
  save ``x'_`y'', replace
  list, sep(0)
}



* ---------------------------------------------------------------------------------------
* Graph settings
* ---------------------------------------------------------------------------------------

* Set graph style to project default settings
*	Pass as arguments: height width (default 3.5in 6.5in)
do "$SSDIMed/scripts/_auxiliary/_project_grstyle.do" 3in 3in

local red "179 0 0"
local green "75 115 47"
local blue "48 84 150"
local bluegray "126 153 180"
local brown "140 045 004"
local orange "237 125 49"
local tan "210 180 140"

* Unemployment rate
local p 1
local color`p' `bluegray'
* local unemp_color "`bluegray'"
grstyle set color "`color`p''": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern dash: p`p'
grstyle set linewidth 1.3pt: p`p'

* Entry
local p 2
local color`p' `brown'
grstyle set color "`color`p''": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.8pt: p`p'

* Medical spending
local p 3
local color`p' black
grstyle set color "`color`p''": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.8pt: p`p'
* shaded region for confidence intervals
grstyle set color "`color`p''%20": p`p'area
grstyle set color "`color`p''%20": p`p'arealine
grstyle set linewidth vvthin: p`p'area

* Mortality
local p 4
local color`p' `green'
grstyle set color "`color`p''": p`p'
grstyle set symbol i: p`p'
grstyle set symbolsize 2.2pt: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth 1.8pt: p`p'
* shaded region for confidence intervals
grstyle set color "`color`p''%20": p`p'area
grstyle set color "`color`p''%20": p`p'arealine
grstyle set linewidth vvthin: p`p'area

* Arrows and lines
local p 5
grstyle set color gs8: p`p'
grstyle set lpattern solid: p`p'
grstyle set linewidth vvthin: p`p'


* -------------------------------------------------------------------------------------------------
* Figure: DI entrant characteristics, by year of Medicare entry
* -------------------------------------------------------------------------------------------------

* Load data
local x covstart_year
use ``x'_tot_pmt', clear
merge 1:1 var using ``x'_died_adj', assert(match) nogen noreport
merge 1:1 `x' using ``x'_incidence', assert(match using) nogen noreport
list, sep(0)
assert inrange(`x', 1993, 2017)

* xvar is month of entry, taken to be two years before Medicare coverage starts
cap drop xvar
gen double xvar = `x' - 2
assert inrange(floor(xvar), 1993 - 2, 2017 - 2)
qui glevelsof xvar
assert r(J) == (2017 - 1993 + 1)


* ------------------------------
* Panel (a): Monthly DI incidence
if 1 {

* Make min and max labels align on both y axes
* Stata tip 93: Handling multiple y axes on twoway graphs: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101000411

* How many major ticks?
* How much should range extend beyond ticks (as fraction of tick step size)
local y_ticks 6
local y_margin_lo 0.55
local y_margin_hi 0.6

* y1 tick lo, step size, format
local y1_tick_lo 260
local y1_step 25
local y1_fmt "%9.2g"
local y1_unit ""

* y2 tick lo, step size, format
local y2_tick_lo 4
local y2_step = 1
local y2_fmt "%9.2g"
local y2_unit "%"

* Set to alt to swap axes
* local yalt
local yalt alt

* Construct y labels and scales from parameters above
forvalues y = 1/2 {
  local y`y'_tick_hi  = `y`y'_tick_lo' + `y`y'_step' * (`y_ticks' - 1)
  local y`y'_range_lo = `y`y'_tick_lo' - `y`y'_step' * `y_margin_lo'
  local y`y'_range_hi = `y`y'_tick_hi' + `y`y'_step' * `y_margin_hi'
  di "y`y'_tick_hi:  `y`y'_tick_hi'"
  di "y`y'_range_lo: `y`y'_range_lo'"
  di "y`y'_range_hi: `y`y'_range_hi'"
  
  local y`y'_label ylabel(`y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi', axis(`y') format("`y`y'_fmt'") noticks nolabels labgap(1pt)) 
  local y`y'_scale yscale(`yalt' range(`y`y'_range_lo' `y`y'_range_hi') axis(`y') noline) 
  di `"y`y'_label:  `y`y'_label'"'
  di `"y`y'_scale:  `y`y'_scale'"'
}

* x-axis settings
local xlo 1987
local xhi 2018
local xscale xscale(range(`xlo' `xhi'))
local x_axis xlabel(1991(4)2015) `xscale' xtitle(Year of entry, margin(t=+.5))

* style and color for y-axis title/labels
local y1_title_style it
local y1_title_color gs8
local y1_label_style it
local y1_label_color gs8
local y1_label_size 9pt

local y2_title_style it
local y2_title_color "`color1'"
local y2_label_style it
local y2_label_color "`color1'"
local y2_label_size 

local text_style sf
local text_color black

* y-axis labels placed above grid rule (build manually)
local y1_x `xlo'
local y1_place ne
local y1_just left
local y2_x `xhi'
local y2_place nw
local y2_just right

forvalues y = 1/2 {
  local y`y'_la
  foreach tick of numlist  `y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi' {
    if `tick' == `y`y'_tick_hi' {
      * white textbox to go behind label
      local y`y'_la `y`y'_la' text(`=`tick' + `y`y'_step'/100' `y`y'_x' " ", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') width(5) height(3) bcolor(white%70))
      *label
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'`y`y'_unit'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
    else {
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
  }
}

* y-axis titles, horizontal orientation (build manually)
local y 1
local y1title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xlo' "{`y`y'_title_style':Entrants per million residents}"
local y1title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') width(5) bcolor(white) margin(b=+.5))
local y1title `y`y'title' ytitle(" ", axis(`y') margin(l=-8))
local y 2
local y2title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xhi' "{`y`y'_title_style':Unemployment rate}"
local y2title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y2title `y`y'title' ytitle(" ", axis(`y') margin(r=-8))

* y-axis settings
local y_axis1 `y1_label' `y1_la' `y1_scale' `y1title'
local y_axis2 `y2_label' `y2_la' `y2_scale' `y2title'

local y1 incidence
local y2 nat_unemp_mean

local line_y1 (connected `y1' xvar, yaxis(1) pstyle(p2))
local line_y2 (connected `y2' xvar, yaxis(2) pstyle(p1))

* Label line_y1
local text_1 text(345 2001.7 "{`text_style':Monthly DI entry}"
local text_1 `text_1', yaxis(1) place(nw) just(right) box lwidth(none) width(30) color(`text_color') bcolor(white%90))
local xref 2002
qui sum `y1' if xvar == `xref'
local pci_1 (pci `=r(mean)' `=`xref'' `=r(mean) + 5' `=`xref' - 0.3', pstyle(p5))

* Label line_y2
local text_2          text(4.1 2006.5 "{`y2_title_style':National unemployment}",  yaxis(2) place(n) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local text_2 `text_2' text(4.1 2006.5 "{`y2_title_style':at time of application}", yaxis(2) place(s) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local xref 2005
qui sum `y2' if xvar == `xref'
local pci_2 (pci `=r(mean)' `=`xref'' `=r(mean) - .65' `=`xref' - 1', yaxis(2) pstyle(p5))

local legend legend(off)
local region graphregion(margin(t=5 b=-1))

twoway `line_y2' `line_y1'  `pci_1' `pci_2', `x_axis' `y_axis1' `y_axis2' `legend' `region' `text_1' `text_2'

* Export graph
cap mkdir "$SSDIMed/results/figures"
graph export "$SSDIMed/results/figures/entry_year_cohort_count-`sample'.pdf", as(pdf) fontface("Source Sans Pro") fontdir("$SSDIMed/data/raw/fonts") replace

}


* ------------------------------
* Panel (b): DI entrant cohort annual spending (tot_pmt)
if 1 {

* Make min and max labels align on both y axes
* Stata tip 93: Handling multiple y axes on twoway graphs: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101000411

* How many major ticks?
* How much should range extend beyond ticks (as fraction of tick step size)
local y_ticks 6
local y_margin_lo 0.55
local y_margin_hi 0.6

* y1 tick lo, step size, format
local y1_tick_lo 13.0
local y1_step 0.2
local y1_fmt "%9.1f"
local y1_sign "$"
local y1_unit " thousand"

* y2 tick lo, step size, format
local y2_tick_lo 4
local y2_step = 1
local y2_fmt "%9.2g"
local y2_sign ""
local y2_unit "%"

* Set to alt to swap axes
local yalt
* local yalt alt

* Construct y labels and scales from parameters above
forvalues y = 1/2 {
  local y`y'_tick_hi  = `y`y'_tick_lo' + `y`y'_step' * (`y_ticks' - 1)
  local y`y'_range_lo = `y`y'_tick_lo' - `y`y'_step' * `y_margin_lo'
  local y`y'_range_hi = `y`y'_tick_hi' + `y`y'_step' * `y_margin_hi'
  di "y`y'_tick_hi:  `y`y'_tick_hi'"
  di "y`y'_range_lo: `y`y'_range_lo'"
  di "y`y'_range_hi: `y`y'_range_hi'"
  
  local y`y'_label ylabel(`y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi', axis(`y') format("`y`y'_fmt'") noticks nolabels labgap(1pt)) 
  local y`y'_scale yscale(`yalt' range(`y`y'_range_lo' `y`y'_range_hi') axis(`y') noline) 
  di `"y`y'_label:  `y`y'_label'"'
  di `"y`y'_scale:  `y`y'_scale'"'
}

* x-axis settings
local xlo 1987
local xhi 2018
local xscale xscale(range(`xlo' `xhi'))
local x_axis xlabel(1991(4)2015) `xscale' xtitle(Year of entry, margin(t=+.5))

* style and color for y-axis title/labels
local y1_title_style it
local y1_title_color gs8
local y1_label_style it
local y1_label_color gs8
local y1_label_size 9pt

local y2_title_style it
local y2_title_color "`color1'"
local y2_label_style it
local y2_label_color "`color1'"
local y2_label_size 

local text_style sf
local text_color black

* y-axis labels placed above grid rule (build manually)
local y1_x `xlo'
local y1_place ne
local y1_just left
local y1_width 30
local y2_x `xhi'
local y2_place nw
local y2_just right
local y2_width 5

forvalues y = 1/2 {
  local y`y'_la
  foreach tick of numlist  `y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi' {
    if `tick' == `y`y'_tick_hi' {
      * white textbox to go behind label
      local y`y'_la `y`y'_la' text(`=`tick' + `y`y'_step'/100' `y`y'_x' " ", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') width(`y`y'_width') height(4) bcolor(white%70))
      *label
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`y`y'_sign'`=string(`tick', "`y`y'_fmt'")'`y`y'_unit'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
    else {
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`y`y'_sign'`=string(`tick', "`y`y'_fmt'")'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
  }
}

* y-axis titles, horizontal orientation (build manually)
local y 1
local y1title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xlo' "{`y`y'_title_style':Medical spending}"
local y1title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y1title `y`y'title' ytitle(" ", axis(`y') margin(l=-8))
local y 2
local y2title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xhi' "{`y`y'_title_style':Unemployment rate}"
local y2title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y2title `y`y'title' ytitle(" ", axis(`y') margin(r=-8))

* y-axis settings
local y_axis1 `y1_label' `y1_la' `y1_scale' `y1title'
local y_axis2 `y2_label' `y2_la' `y2_scale' `y2title'

* Plot layers
local y1 tot_pmt
local y2 nat_unemp_mean
assert clustvar == "`clust'" if !missing(clustvar)

local line_y1 (connected `y1' xvar if clustvar == "`clust'", yaxis(1) pstyle(p3))
local ci_area (rarea `y1'_ci_lo `y1'_ci_hi xvar if clustvar == "`clust'", sort pstyle(p3area))
local line_y2 (connected `y2' xvar, yaxis(2) pstyle(p1))

* Label line_y1
local text_1 text(13.9 2000.2 "{`text_style':Annual medical}" "{`text_style':spending}"
local text_1 `text_1', yaxis(1) place(c) just(center) box lwidth(none) width(20) color(`text_color') bcolor(white%90))
local xref 2005
qui sum `y1' if xvar == `xref'
local pci_1 (pci `=r(mean)' `=`xref'' `=r(mean) + .12' `=`xref' - 1.7', pstyle(p5))

* Label line_y2
local text_2          text(4.1 2006.5 "{`y2_title_style':National unemployment}",  yaxis(2) place(n) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local text_2 `text_2' text(4.1 2006.5 "{`y2_title_style':at time of application}", yaxis(2) place(s) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local xref 2005
qui sum `y2' if xvar == `xref'
local pci_2 (pci `=r(mean)' `=`xref'' `=r(mean) - .65' `=`xref' - 1', yaxis(2) pstyle(p5))

local legend legend(off)
local region graphregion(margin(t=5 b=-1))

twoway `ci_area' `line_y1' `line_y2' `pci_1' `pci_2', `x_axis' `y_axis1' `y_axis2' `legend' `region' `text_1' `text_2'

* Export graph
cap mkdir "$SSDIMed/results/figures"
graph export "$SSDIMed/results/figures/entry_year_cohort_tot_pmt_clust-`clust'-`sample'.pdf", as(pdf) fontface("Source Sans Pro") fontdir("$SSDIMed/data/raw/fonts") replace

}


* ------------------------------
* Panel (c): DI entrant cohort mortality (died)
if 1 {

* Make min and max labels align on both y axes
* Stata tip 93: Handling multiple y axes on twoway graphs: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1101000411

* How many major ticks?
* How much should range extend beyond ticks (as fraction of tick step size)
local y_ticks 6
local y_margin_lo 0.55
local y_margin_hi 0.6

* y1 tick lo, step size, format
local y1_tick_lo 265
local y1_step 5
local y1_fmt "%9.2g"
local y1_unit ""

* y2 tick lo, step size, format
local y2_tick_lo 4
local y2_step = 1
local y2_fmt "%9.2g"
local y2_unit "%"

* Set to alt to swap axes
local yalt
* local yalt alt

* Construct y labels and scales from parameters above
forvalues y = 1/2 {
  local y`y'_tick_hi  = `y`y'_tick_lo' + `y`y'_step' * (`y_ticks' - 1)
  local y`y'_range_lo = `y`y'_tick_lo' - `y`y'_step' * `y_margin_lo'
  local y`y'_range_hi = `y`y'_tick_hi' + `y`y'_step' * `y_margin_hi'
  di "y`y'_tick_hi:  `y`y'_tick_hi'"
  di "y`y'_range_lo: `y`y'_range_lo'"
  di "y`y'_range_hi: `y`y'_range_hi'"
  
  local y`y'_label ylabel(`y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi', axis(`y') format("`y`y'_fmt'") noticks nolabels labgap(1pt)) 
  local y`y'_scale yscale(`yalt' range(`y`y'_range_lo' `y`y'_range_hi') axis(`y') noline) 
  di `"y`y'_label:  `y`y'_label'"'
  di `"y`y'_scale:  `y`y'_scale'"'
}

* x-axis settings
local xlo 1987
local xhi 2018
local xscale xscale(range(`xlo' `xhi'))
local x_axis xlabel(1991(4)2015) `xscale' xtitle(Year of entry, margin(t=+.5))

* style and color for y-axis title/labels
local y1_title_style it
local y1_title_color gs8
local y1_label_style it
local y1_label_color gs8
local y1_label_size 9pt

local y2_title_style it
local y2_title_color "`color1'"
local y2_label_style it
local y2_label_color "`color1'"
local y2_label_size 

local text_style sf
local text_color black

* y-axis labels placed above grid rule (build manually)
local y1_x `xlo'
local y1_place ne
local y1_just left
local y2_x `xhi'
local y2_place nw
local y2_just right

forvalues y = 1/2 {
  local y`y'_la
  foreach tick of numlist  `y`y'_tick_lo'(`y`y'_step')`y`y'_tick_hi' {
    if `tick' == `y`y'_tick_hi' {
      * white textbox to go behind label
      local y`y'_la `y`y'_la' text(`=`tick' + `y`y'_step'/100' `y`y'_x' " ", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') width(5) height(3) bcolor(white%70))
      *label
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'`y`y'_unit'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
    else {
      local y`y'_la `y`y'_la' text(`tick' `y`y'_x' "{`y`y'_label_style':`=string(`tick', "`y`y'_fmt'")'}", yaxis(`y') place(`y`y'_place') just(`y`y'_just') size(`y`y'_label_size') box lwidth(none) color(`y`y'_label_color') bcolor(none))
    }
  }
}

* y-axis titles, horizontal orientation (build manually)
local y 1
local y1title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xlo' "{`y`y'_title_style':Deaths per 10,000}"
local y1title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y1title `y`y'title' ytitle(" ", axis(`y') margin(l=-8))
local y 2
local y2title text(`=`y`y'_tick_lo' + `y`y'_step'*(`y_ticks' - 1 + `y_margin_hi')' `xhi' "{`y`y'_title_style':Unemployment rate}"
local y2title `y`y'title', yaxis(`y') place(`y`y'_place') just(`y`y'_just') box lwidth(none) color(`y`y'_title_color') bcolor(white) margin(b=+.5))
local y2title `y`y'title' ytitle(" ", axis(`y') margin(r=-8))

* y-axis settings
local y_axis1 `y1_label' `y1_la' `y1_scale' `y1title'
local y_axis2 `y2_label' `y2_la' `y2_scale' `y2title'

* Plot layers
local y1 died_adj
local y2 nat_unemp_mean
assert clustvar == "`clust'" if !missing(clustvar)

local line_y1 (connected `y1' xvar if clustvar == "`clust'", yaxis(1) pstyle(p4))
local ci_area (rarea `y1'_ci_lo `y1'_ci_hi xvar if clustvar == "`clust'", sort pstyle(p4area))
local line_y2 (connected `y2' xvar, yaxis(2) pstyle(p1))

* Label line_y1
local text_1          text(287.5 2002 "{`text_style':Annual}",    yaxis(1) place(n) just(center) box lwidth(none) width(15) color(`text_color') bcolor(white%90))
local text_1 `text_1' text(287.5 2002 "{`text_style':mortality}", yaxis(1) place(s) just(center) box lwidth(none) width(15) color(`text_color') bcolor(white%90))
local xref 1999
qui sum `y1' if xvar == `xref'
local pci_1 (pci `=r(mean)' `=`xref'' `=r(mean) + 3.5' `=`xref' + 2', pstyle(p5))

* Label line_y2
local text_2          text(4.1 2006.5 "{`y2_title_style':National unemployment}",  yaxis(2) place(n) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local text_2 `text_2' text(4.1 2006.5 "{`y2_title_style':at time of application}", yaxis(2) place(s) just(center) box lwidth(none) size(*0.85) width(35) color(`y2_title_color') bcolor(white%90))
local xref 2005
qui sum `y2' if xvar == `xref'
local pci_2 (pci `=r(mean)' `=`xref'' `=r(mean) - .65' `=`xref' - 1', yaxis(2) pstyle(p5))

local legend legend(off)
local region graphregion(margin(t=5 b=-1))

twoway `ci_area' `line_y1' `line_y2' `pci_1' `pci_2', `x_axis' `y_axis1' `y_axis2' `legend' `region' `text_1' `text_2'

* Export graph
cap mkdir "$SSDIMed/results/figures"
graph export "$SSDIMed/results/figures/entry_year_cohort_died_clust-`clust'-`sample'.pdf", as(pdf) fontface("Source Sans Pro") fontdir("$SSDIMed/data/raw/fonts") replace

}





** EOF
